package com.jpa.many.demo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName Test2
 * @description: TODO
 * @author: 朱同学
 * @Date 2021/4/12 22:15
 **/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "entityManagerFactoryTest2",
        transactionManagerRef = "transactionManagerTest2",
        basePackages = {"com.jpa.many.demo.test2"})
public class Test2Config {


    @Resource
    private Environment env;

    @Autowired
    @Qualifier("test2DataSource")
    private DataSource primaryDataSource;

    @Bean(name = "entityManagerTest2")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
    }

    @Bean(name = "entityManagerFactoryTest2")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
        //  pagkages 设置实体类所在位置
        return builder
                .dataSource(primaryDataSource)
                .properties(getVendorProperties(primaryDataSource))
                .packages("com.jpa.many.demo.test2.model")
                .persistenceUnit("primaryPersistenceUnit")
                .build();
    }

    @Autowired
    private JpaProperties jpaProperties;

    private Map<String, String> getVendorProperties(DataSource dataSource) {
        //return jpaProperties.getHibernateProperties(dataSource);

        Map<String, String> jpaMap = new HashMap<>(16);
        jpaMap.put("hibernate.hbm2ddl.auto", "update");
        jpaMap.put("hibernate.show_sql", env.getProperty("spring.jpa.show-sql"));
        jpaMap.put("hibernate.format_sql", env.getProperty("spring.jpa.hibernate.format_sql"));
        jpaMap.put("hibernate.dialect", env.getProperty("spring.jpa.hibernate.primary-dialect"));
        jpaMap.put("hibernate.current_session_context_class", "org.springframework.orm.hibernate5.SpringSessionContext");

        jpaProperties.setGenerateDdl(true);
        jpaProperties.setShowSql(true);
        jpaProperties.setProperties(jpaMap);

        return jpaProperties.getProperties();
    }

    @Bean(name = "transactionManagerTest2")
    public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
    }


}
